package com.anjubao.parking.municipal.parkinglot.service;

import com.anjubao.parking.municipal.parkinglot.dto.ParkingRecordCostTimeStatDto;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingStatServiceDto;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingLotMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingRecordMapper;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingTradeOrderMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingRecord;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/**
 * 停车记录表Service
 *
 * @author zhuchunfeng
 */
@Service
public class ParkingRecordService extends ServiceImpl<ParkingRecordMapper, ParkingRecord>{

    @Autowired
    private ParkingRecordMapper parkingRecordMapper;
    @Autowired
    private ParkingTradeOrderMapper parkingTradeOrderMapper;
    @Autowired
    private ParkingLotMapper parkingLotMapper;

    /**
     * 服务数据
     * @param cityId
     * @param startTime
     * @param endTime
     * @return
     */
    public ParkingStatServiceDto getParkingStat(Integer cityId, LocalDateTime startTime, LocalDateTime endTime) {
        //查询费用数据
        ParkingStatServiceDto parkingStat = parkingTradeOrderMapper.getParkingStat(cityId, startTime, endTime);
        //查询泊位数据
        Map<String,Object> map=parkingLotMapper.getParkingStat(cityId);
        //计算泊位使用率
        DecimalFormat decimalFormat=new DecimalFormat("##.00%");
        String format = decimalFormat.format((float) Integer.parseInt(map.get("used").toString()) / (float) Integer.parseInt(map.get("total").toString()));
        parkingStat.setSlotUtilization(format);
        parkingStat.setOpenSlot(Integer.parseInt(map.get("openSlot").toString()));
        parkingStat.setComplaintsNum(20);
        parkingStat.setEndComplaints("99%");
        parkingStat.setAttendanceNum("145/150");
        return parkingStat;
    }

    /**
     * 统计停车时长占比（流量）
     *
     * @param cityId    城市ID
     * @param startTime 开始统计时间
     * @param endTime   结束统计时间
     * @return   停车时长占比
     */
    public List<ParkingRecordCostTimeStatDto> getParkingCostTimeStat(Integer cityId, LocalDateTime startTime, LocalDateTime endTime) {

        //数据库获取数据
        List<ParkingRecordCostTimeStatDto> costTimeStatDtoList = parkingRecordMapper.queryParkingCostTimeStat(cityId, startTime, endTime);

        //获取 0, 30, 60 数据
        List<ParkingRecordCostTimeStatDto> collect = costTimeStatDtoList.stream()
                .filter(f -> f.getParkingCostTimeGe() < 90)  //获取 0, 30, 60 数据
                .map(f -> f.setParkingCostTimeLt(f.getParkingCostTimeGe() + 30)) //补全信息数据
                .sorted(Comparator.comparingInt(ParkingRecordCostTimeStatDto::getParkingCostTimeGe)) //排序
                .collect(toList());

        //获取90以上的数据 并追加到列表
        List<ParkingRecordCostTimeStatDto> costTimeGe90 = costTimeStatDtoList.stream().filter(f -> f.getParkingCostTimeGe() >= 90).collect(toList());
        Integer totalGe90 = costTimeGe90.stream().mapToInt(ParkingRecordCostTimeStatDto::getParkingRecordTotal).sum();
        if (totalGe90 > 0) {
            collect.add(new ParkingRecordCostTimeStatDto().setParkingCostTimeGe(90).setParkingRecordTotal(totalGe90));
        }

        return collect;
    }
}
